<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Db;
use app\admin\model\Order;
use app\admin\model\Yuyue;
use app\admin\model\Store;
use app\admin\model\Staff;
use app\admin\model\Custom;
use app\admin\model\Item;
use app\admin\model\StoreVisit;
use app\admin\model\CtmSpecial;
use app\admin\model\ProgramRefund;
use app\admin\model\StoreTixian;
use app\admin\model\StaffTixian;
use app\admin\model\StoreCtm;

class IndexController extends CommonController {
    /*
     * 首页
     * author xfwed 
     * date 2019.09.17
     */

    public function index() {
        //总营业额
        $tuikuan = Yuyue::where('status', '=', 6)->sum('money'); //退款金额
        $payMoney = Order::where('status', '>=', 2)->sum('pay_money'); //订单金额
        $data['allMoney'] = $payMoney - $tuikuan;
        $beginTime = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y"))); //本月开始时间
        $endTime = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y"))); //本月结束时间
        $beginTime1 = date('Y-m-01 00:00:00', strtotime('-1 month')); //上月开始时间
        $endTime1 = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day')); //上月结束时间

//        $payMoney1 = Order::where('status', '>=', 2)
//                ->where('pay_time', 'between', [$beginTime, $endTime])
//                ->sum('pay_money'); //本月订单金额
//        $payMoney2 = Order::where('status', '>=', 2)
//                ->where('pay_time', 'between', [$beginTime1, $endTime1])
//                ->sum('pay_money'); //上月订单金额
//        if ($payMoney2 == 0) {
//            $data['moneyGrow'] = 1;
//        } else {
//            $a = bcdiv($payMoney1,$payMoney2,2);
//            $data['moneyGrow'] = bcsub($a,1);
//        }


        //加盟店
        $data['storeNum'] = Store::where('is_del', '=', 1)->where('auditstore',1)->count();
        //$storeNum1 = Store::where('is_del', '=', 1)->where('add_time', 'between', [$beginTime, $endTime])->count();
        //$storeNum2 = Store::where('is_del', '=', 1)->where('add_time', 'between', [$beginTime1, $endTime1])->count();
        //$data['storeGrow'] = $storeNum1 - $storeNum2;

        //美容师
        $data['staffNum'] = Staff::where('staff_on', '=', 1)->where('status',1)->count();
        //$staffNum1 = Staff::where('staff_on', '=', 1)->where('add_time', 'between', [$beginTime, $endTime])->count();
        //$staffNum2 = Staff::where('staff_on', '=', 1)->where('add_time', 'between', [$beginTime1, $endTime1])->count();
        //$data['staffGrow'] = $staffNum1 - $staffNum2;

        //客户数
        $data['ctmNum'] = Custom::where('status', '=', 2)->count();
        $ctmNum1 = Custom::where('status', '=', 2)->where('add_time', 'between', [$beginTime, $endTime])->count();
        //$ctmNum2 = Custom::where('status', '=', 2)->where('add_time', 'between', [$beginTime1, $endTime1])->count();
        //$data['ctmGrow'] = $ctmNum1 - $ctmNum2;
        $data['ctmGrow'] = $ctmNum1;

        //加盟店申请
        //$data['storeNoNum'] = 0;
        //美容师申请
        $data['staffNoNum'] = Staff::where('status', '=', 0)->count();
        //项目退款申请
        //$data['ProgramRefund'] = Yuyue::where('refund_status',1)->count();
        //商品退款申请
        //$data['goodsRefund'] = Db::table('pay_order_goods')->where('is_refund',1)->count();
        //老板提现申请
        $data['storeTixian'] = StoreTixian::where('status',0)->count();
        //美容师提现申请
        //$data['staffTixian'] = StaffTixian::where('status',0)->count();
        //预约异常
        //$data['yuyueNoNum'] = Yuyue::where('status', 'in', '2,3')->where('start_time', '<', date('Y-m-d H:i:s'))->count();
        //项目数量
        $data['itemNum'] = Item::where('is_del', '=', 1)->count();
        //商品数量
        $data['goodsNum'] = Db::table('goods_info')->where('valid', '=', 1)->count();
//        $time = time();    //组合数据
//        $date7 = [];
//        for ($i = 1; $i <= 7; $i++) {
//            $date7[] = date('Y-m-d', strtotime('+' . $i - 7 . ' days', $time));
//        }
//        $data['date7'] = $date7;
//        $date30 = [];
//        for ($i = 1; $i <= 30; $i++) {
//            $date30[] = date('Y-m-d', strtotime('+' . $i - 30 . ' days', $time));
//        }
//        $data['date30'] = $date30;
//        foreach ($date7 as $k => $v) {
//            $Time1 = $v . ' 00:00:00';
//            $Time2 = $v . ' 23:59:59';
//            $storeVisit7[] = StoreVisit::where('add_time', 'between', [$Time1,$Time2])->count();
//            $yuyueCtmNum7[]= Yuyue::where('add_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $serCtmNum7[]= Yuyue::where('staff_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $serNum7[]= Yuyue::where('staff_time', 'between', [$Time1,$Time2])->count();
//            $orderCtmNum7[] = Order::where('add_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $orderNum7[] = Order::where('add_time', 'between', [$Time1,$Time2])->count();
//        }
//        $data['storeVisit7'] = $storeVisit7;
//        $data['yuyueCtmNum7'] = $yuyueCtmNum7;
//        $data['serCtmNum7'] = $serCtmNum7;
//        $data['serNum7'] = $serNum7;
//        $data['orderCtmNum7'] = $orderCtmNum7;
//        $data['orderNum7'] = $orderNum7;
        
//        foreach ($date30 as $k => $v) {
//            $Time1 = $v . ' 00:00:00';
//            $Time2 = $v . ' 23:59:59';
//            $storeVisit30[] = StoreVisit::where('add_time', 'between', [$Time1,$Time2])->count();
//            $yuyueCtmNum30[]= Yuyue::where('add_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $serCtmNum30[]= Yuyue::where('staff_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $serNum30[]= Yuyue::where('staff_time', 'between', [$Time1,$Time2])->count();
//            $orderCtmNum30[] = Order::where('add_time', 'between', [$Time1,$Time2])->group('ctm_id')->count();
//            $orderNum30[] = Order::where('add_time', 'between', [$Time1,$Time2])->count();
//        }
//        $data['storeVisit30'] = $storeVisit30;
//        $data['yuyueCtmNum30'] = $yuyueCtmNum30;
//        $data['serCtmNum30'] = $serCtmNum30;
//        $data['serNum30'] = $serNum30;
//        $data['orderCtmNum30'] = $orderCtmNum30;
//        $data['orderNum30'] = $orderNum30;
        //客户类型占比
        //新客户
        $commonCtmList = Custom::where('vip_status','=',1)->field('id')->select();
        $specialCtmList = Custom::where('vip_status','=',2)->field('id')->select();
        $data['newCtmNum'] = Custom::where('vip_status','=',0)->count();
        $data['commonCtmNum'] = count($commonCtmList);
        $data['specialCtmNum'] = count($specialCtmList);
//        $commonCtmNum2 = 0;
//        foreach ($commonCtmList as $k=>$v){
//            $num = Order::where('ctm_id','=',$v['id'])->where('status','=',2)->count();
//            if($num > 2){
//                $commonCtmNum2 = $commonCtmNum2 + 1;
//            }
//        }
//        $data['commonCtmNum2'] = $commonCtmNum2;
//        
//        $specialCtmNum2 = 0;
//        foreach ($specialCtmList as $k=>$v){
//            $end_time = CtmSpecial::where('ctm_id','=',$v['id'])->value('end_time');
//            $num = Order::where('ctm_id','=',$v['id'])
//                    ->where('status','=',2)
//                    ->where('pay_time','>',$end_time)
//                    ->count();
//            if($num > 0){
//                $specialCtmNum2 = $specialCtmNum2 + 1;
//            }
//        }
//        $data['specialCtmNum2'] = $specialCtmNum2;
        
        //客户年龄占比
//        $age = Custom::where('status','=',2)->count();
//        $data['age'] = [$age,0,0,0,0];
        
        //门店销售额
        $data['storeTop'] = Store::field('sum(money+tx_money) money,store_name,add_time,id')
                ->where('is_del', '=', 1)
                ->where('auditstore',1)
                ->group('id')
                ->order('money desc')
                //->limit(0,5)
                ->select();
        foreach ($data['storeTop'] as $k=>$v){
            $data['storeTop'][$k]['serNum'] = Yuyue::where('store_id',$v['id'])->where('status','in','4,7,8')->count();
        }
        //美容师接单量排行
        $yuyueData = Yuyue::alias('y')
                ->join('staff s','s.id = y.staff_id','left')
                ->where('y.status','in','4,7,8')
                ->group('y.staff_id')
                ->field("count(y.id) value,s.staff_name name")
                ->order("value desc")
                ->limit(0,10)
                ->select();
       
        $data['staffData'] = $yuyueData;
        return $this->ajaxReturn(0, '查询成功', $data);
    }
    
    
}
